package com.zttek.photosShareProject.modules.web.share;

import com.baomidou.mybatisplus.plugins.Page;
import com.zttek.photosShareProject.common.base.MyBaseController;
import com.zttek.photosShareProject.common.json.UserVO;
import com.zttek.photosShareProject.modules.model.Share;
import com.zttek.photosShareProject.modules.model.User;
import com.zttek.photosShareProject.modules.service.login.IUserService;
import com.zttek.photosShareProject.modules.service.share.IImageService;
import com.zttek.photosShareProject.modules.service.share.IShareService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Y
 * Date: 2017-08-31
 * Time: 14:56
 */
@RequestMapping("/userF")
@Controller
public class UserFController extends MyBaseController{

    @Autowired
    private IUserService userService;

    @Autowired
    private IImageService imageService;

    @Autowired
    private IShareService shareService;

//    @RequestMapping("/getList")
//    @ResponseBody
//    public Object getList(Long[] userIds) throws Exception{
//        List<User> users = userService.selectUserByIds(userIds);
//        List<UserVO> userVOS = new ArrayList<>();
//        for (User user : users) {
//            userVOS.add(userService.user2VO(user, null));
//        }
//        return renderSuccess(userVOS);
//    }

    @RequestMapping(value = "/getDetail", method = RequestMethod.POST)
    @ResponseBody
    public Object getDetail(@RequestParam("targetId") Long targetId, @RequestParam("userId") Long userId) throws Exception {
        User target = userService.selectUserById(targetId);
        UserVO vo = userService.user2VO(target, userId, true);
        return renderSuccess(vo);
    }

    @RequestMapping(value = "/getStarList", method = RequestMethod.POST)
    @ResponseBody
    public Object getStarList(@RequestBody UserVO.GetListForm form) throws Exception{
        Long[] userIds = (Long[]) shareService.getUserStarList(form.getUserId()).toArray();
        Page<User> page = new Page<>(form.getPageNum(), form.getPageSize(), form.getOrderByField());
        page.setAsc(form.isAscend());
        userService.selectStarPage(page, form.getUserId());
        List<User> users = page.getRecords();
        List<UserVO> userVOS = new ArrayList<>();
        for (User user : users) {
            userVOS.add(userService.user2VO(user, null, true));
        }
        return renderSuccess(userVOS);
    }

    @RequestMapping(value = "/star", method = RequestMethod.POST)
    @ResponseBody
    public Object star(@RequestBody UserVO.RelevanceForm form) throws Exception {
        shareService.star(form.getTargetId(), form.getUserId(), form.isDirection());
        return renderSuccess();
    }


}